package com.person.webdemoproject2.service;

import com.person.webdemoproject2.mapper.UserMapper;
import com.person.webdemoproject2.pojo.UserRelationship;
import com.person.webdemoproject2.utils.jwtUtils;
import com.person.webdemoproject2.utils.sendNoticeUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    sendNoticeUtils sendNoticeUtils;
    @Autowired
    UserMapper userMapper;


    @Override

    public boolean follow(UserRelationship userRelationship, String token) {
        String jwt = token;



            jwtUtils.parseJWT(jwt);

        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");
        String username = (String) claims.get("username");

        userRelationship.setUserid(userid);
        LocalDateTime now = LocalDateTime.now();
        userRelationship.setCreate_time(now);
        userRelationship.setUpdate_time(now);
        userRelationship.setUsername(username);

        List<String> refollow = userMapper.refollow(userRelationship);

        for (String ref : refollow) {
            if (ref.equals(userRelationship.getFusername())) {
                return false;
            }
        }


        userMapper.follow(userRelationship);
       Integer user=userMapper.selectIdByusername(now,userid);
        user = userRelationship.getFollowed_person();
        String type = "follow";
       String content="有用户关注了你";
        sendNoticeUtils.sendNotification(user,userid,type,content);
        return true;
    }
}
